package com.xueqiu.movieplaymanagersystem.utils;

import lombok.extern.slf4j.Slf4j;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author xueqiu
 */
@Slf4j
public class MD5Utils {
    /**
     * MD5加密
     * 作者：
     * 链接：<a href="https://github.com/lxclxc/MD5Utils">...</a>
     * 来源：掘金
     *
     * @param plainPassword 明文密码
     * @return md5密文密码
     */
    public static String encode(String plainPassword) {
        try {
            MessageDigest digest = MessageDigest.getInstance("md5");
            byte[] buffer = digest.digest(plainPassword.getBytes());
            StringBuilder stringBuilder = new StringBuilder();
            for (byte b : buffer) {
                int a = b & 0xff;
                String hex = Integer.toHexString(a);
                //16进制是2位表示，所以给1个位的数的前面添上0
                if (hex.length() == 1) {
                    hex = 0 + hex;
                }
                stringBuilder.append(hex);
            }
            String md5Password = stringBuilder.toString();
            log.info("MD5Utils md5Password ---> {}",md5Password);
            return md5Password;
        } catch (NoSuchAlgorithmException e) {
            log.error("MD5Utils ---> {}", e.getClass());
            e.printStackTrace();
        }
        return null;
    }
}
